#include "khaibao.h"
void Nhap(TINH A[],int n,char ten[])
{
	int i=0,y=0;
	fstream fileOut(ten,ios::out|ios::binary);
	if (!fileOut) //khong mo duoc tap tin
	{
		cout<<"Khong tao duoc tap tin"<<ten<<endl;
		exit(0);
	}
	for (i=0;i<n;i++)
	{
		cout<<"\n\n----------------Tinh "<<y++<<" ----------------";
		cout<<"\nNhap vao Ma Tinh: ";
		cin>>A[i].MaTinh;
		cout<<"\nNhap vao Ten Tinh: ";
		cin.getline(A[i].TenTinh,30);
		cout<<"\nNhap vao Dan So: ";
		cin>>A[i].DanSo;
		cout<<"\nNhap vao Dien Tich: ";
		cin>>A[i].DienTich;
		fileOut.write((char *)(&A[i]),sizeof(TINH));
	}
	fileOut.close();
}

void Xuat(char ten[])
{
	int i=0;
	TINH t;
	fstream fileIn(ten,ios::in|ios::binary);
	if (!fileIn) //khong mo duoc tap tin
	{
		cout<<"Khong tao duoc tap tin"<<ten<<endl;
		exit(1);
	}
	cout<<"\tMa Tinh\t\tTen Tinh\tDan So\tDien Tich\n";
	while (1)
	{
		fileIn.read((char *)(&t),sizeof(TINH));
		if(!fileIn)
			break;
		cout<<"\t"<<t.MaTinh<<"\t\t"<<t.TenTinh<<"\t\t"<<t.DanSo<<"\t\t"<<t.DienTich;
		cout<<endl;
	}
}
// cau a
void Xuat_dslon_1000(TINH A[],int n)
{
	TINH t;
	cout<<"Nhung Tinh co dan so lon hon 1000000 la: "<<endl;
	for (int i = 0; i< n; i++)
	{
		if(A[i].DanSo>1000000)
			cout<<"\t"<<t.MaTinh<<"\t\t"<<t.TenTinh<<"\t\t"<<t.DanSo<<"\t\t"<<t.DienTich;
		cout<<endl;
	}
}
//cau b
void Xuat_dt_max(TINH A[],int n)
{
	float max=A[0].DienTich;
	TINH t;
	for(int i=1; i<n; i++)
	{
		if(A[i].DienTich>max)
			max=A[i].DienTich;
	}
	cout<<"\nTinh co dien tich lon nhat: \n";
	cout<<"\tMa Tinh\t\tTen Tinh\tDan So\tDien Tich\n";
	for (int i = 0; i< n; i++)
	{
		if(A[i].DienTich>max)
			cout<<"\t"<<t.MaTinh<<"\t\t"<<t.TenTinh<<"\t\t"<<t.DanSo<<"\t\t"<<t.DienTich;
		cout<<endl;
	}

	
}
//cau c

void sapxeptinh_giam_dt(TINH A[],int n)
{
	TINH tam;
	for(int i=0;i<n-1;i++)
		for(int j=i+1;j<n;j++)
		{
			if(A[i].DienTich<A[j].DienTich)
			{
				tam=A[i];
				A[i]=A[j];
				A[j]=tam;
			}
		}
	cout<<"\nDanh sach cac tinh da duoc sap xep giam dan theo dien tich:";
	cout<<"\tMa Tinh\t\tTen Tinh\tDan So\tDien Tich\n";
	for(int i=0; i<n; i++)
	{
		cout<<"\t"<<A[i].MaTinh<<"\t\t"<<A[i].TenTinh<<"\t\t"<<A[i].DanSo<<"\t\t"<<A[i].DienTich<<endl;
	}
}
//cau d
void Xuat_tinh_giang_max(TINH A[],int n)
{
	float max=A[0].DienTich;
	TINH t;
	for(int i=1; i<n; i++)
	{
		if((A[i].DienTich>max)&&(strcmp (A[i].TenTinh, " Giang ")==0))
			max=A[i].DienTich;
	}
	cout<<"\nTinh co dien tich lon nhat: \n";
	cout<<"\tMa Tinh\t\tTen Tinh\tDan So\tDien Tich\n";
	for (int i = 0; i< n; i++)
	{
		if(A[i].DienTich==max)
			cout<<"\t"<<t.MaTinh<<"\t\t"<<t.TenTinh<<"\t\t"<<t.DanSo<<"\t\t"<<t.DienTich;
		cout<<endl;
	}

	
}
//cau e
void matdodansomax(TINH A[],int n)
{
	TINH t;
	float matdomax=A[0].DanSo/A[0].DienTich;
	for(int i=1; i<n; i++)
	{
		if(A[i].DanSo/A[i].DienTich>matdomax)
			matdomax=A[i].DienTich;
	}

	cout<<"\nTinh co mat do dan so lon nhat: \n";
	cout<<"\tMa Tinh\t\tTen Tinh\tDan So\tDien Tich\n";
	for(int i=0; i<n; i++)
	{
		if(A[i].DienTich==matdomax)
			cout<<"\t"<<t.MaTinh<<"\t\t"<<t.TenTinh<<"\t\t"<<t.DanSo<<"\t\t"<<t.DienTich<<endl;
	}
}